//
//  LLGStatuseTableViewCell.swift
//  Weibo30LLG
//
//  Created by 李龙 on 16/8/12.
//  Copyright © 2016年 lilong. All rights reserved.
//

import UIKit
import SnapKit

//间距
let StatuseTableViewCellMargin: CGFloat = 10

class LLGStatuseTableViewCell: UITableViewCell {
    
    //记录toolBar的顶部约束
    var toolBarTopConstraint: Constraint?
    
    //数据viewModel
    var statuseViewModel: LLGStatuseViewModel? {
    
        //设置子控件的参数
        didSet {
        
            originView.statuseViewModel = statuseViewModel
            toolBar.statuseViewModel = statuseViewModel
            
            //取消约束
            toolBarTopConstraint?.uninstall()
            
            if statuseViewModel?.statuse?.retweeted_status != nil {
            
                //如果有值,显示转发微博
                retweetView.hidden = false
                
                //更新约束
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
                retweetView.statuseViewModel = statuseViewModel
                
            } else {
                
                //隐藏
                retweetView.hidden = true
                
                //更新约束
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(originView.snp_bottom).constraint
                })
            }
        }
    }
    

    //用户视图
    private lazy var originView: LLGStatuseOriginView = {
    
        let view = LLGStatuseOriginView()
        return view
    }()
    
    //转发微博
    private lazy var retweetView: LLGStatuseRetweetView = {
    
        let view = LLGStatuseRetweetView()
        return view
    }()
    
    //toolBar
    private lazy var toolBar: LLGStatuseToolBar = LLGStatuseToolBar()
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: --添加子控件,设置约束
    private func setupUI() {
        
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
    
        //添加子控件
        contentView.addSubview(originView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBar)
        
        //设置约束
        originView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(8)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originView.snp_bottom)
            make.leading.trailing.equalTo(originView)
        }
        
        toolBar.snp_makeConstraints { (make) -> Void in
            self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(35)
        }
        
        //设置contentView约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolBar)
            make.top.leading.trailing.equalTo(self)
        }
    }

}
